package javaclass;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;


@Aspect
@Component
public class UltisLog {
    @Pointcut("execution(public * javaclass.*.*(..))")
    public void aaa(){

    }
    @Before("aaa()")
    public void before(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println("开始位置,我作用的目标方法是："+name+"，参数是："+ Arrays.asList(args));
    }

    @AfterReturning(value="aaa()",returning = "result")
    public void afterreturn(JoinPoint joinPoint,Object result){
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println("返回值位置,我作用的目标方法是："+name+"，参数是："+Arrays.asList(args)+"结果是："+result);
    }
    @AfterThrowing(value="aaa()",throwing = "exception")
    public void afterthrows(JoinPoint joinPoint,Exception exception){
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println("异常位置,我作用的目标方法是："+name+"，参数是："+Arrays.asList(args)+"异常信息是："+exception);
    }
    @After("aaa()")
    public void after(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println("结束位置,我作用的目标方法是："+name+"，参数是："+ Arrays.asList(args));
    }
    @Around("aaa()")
    public static Object arround(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        Signature signature = pjp.getSignature();
        String name = signature.getName();
        Object proceed = null;
        try {
            System.out.println("开始位置,我作用的目标方法是："+name+"，参数是："+ Arrays.asList(args));
            proceed = pjp.proceed();
            System.out.println("返回位置,我作用的目标方法是："+name+"，参数是："+ Arrays.asList(args)+"结果是："+proceed);
        }catch (Exception e){
            System.out.println("异常位置,我作用的目标方法是："+name+"，参数是："+Arrays.asList(args)+"异常信息是："+e);
        }finally {
            System.out.println("结束位置,我作用的目标方法是："+name+"，参数是："+ Arrays.asList(args));
        }
        //Object proceed = pjp.proceed();

        return proceed;
    }
}
